package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.GatewayDTO;
import com.hksj.Intelligentmanagement.entity.*;
import com.hksj.Intelligentmanagement.mapper.*;
import com.hksj.Intelligentmanagement.vo.DeviceVo;
import com.hksj.Intelligentmanagement.vo.GatewayVo;
import com.hksj.Intelligentmanagement.vo.MqttAuthVo;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author zengke
 * @ClassName GatewayService
 * @Date 2023/3/23 14:05
 * @Version 1.0
 * @Description TODO
 */

/**
 * 网关业务
 */
@Service
public class GatewayService {

    @Autowired
    private GatewayMapper gatewayMapper;

    @Autowired
    private Mapper mapper;

    @Autowired
    private GatewayAuthMapper gatewayAuthMapper;

    @Autowired
    private GatewayDeviceMapper gatewayDeviceMapper;

    @Autowired
    private MqttAuthMapper mqttAuthMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    //新增网关
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result createGateway(GatewayDTO gatewayDTO){

        //查询数据库中是否存在重复网关
        QueryWrapper<GatewayEntity> wrapper = new QueryWrapper<GatewayEntity>()
                .eq("sn_id", gatewayDTO.getSnId());

        GatewayEntity gatewayEntity = gatewayMapper.selectOne(wrapper);

        if (!Objects.isNull(gatewayEntity)){
            //不为空
            return Result.error("网关设备已存在,请勿重复添加");
        }

        //为空,创建网关
        //使用dozer把gatewayDTO转为gatewayEntity
        gatewayEntity = mapper.map(gatewayDTO, GatewayEntity.class);

        //插入网关
        gatewayMapper.insert(gatewayEntity);

        return Result.ok("新增网关成功");
    }

    //删除网关
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deleteGateway(String id){

        //查询设备在数据库中是否存在
        GatewayEntity gatewayEntity = gatewayMapper.selectById(id);

        if (Objects.isNull(gatewayEntity)){
            //不存在
            return Result.error("输入的网关id有误,删除失败");
        }

        //删除网关和mqtt认证参数的拓扑关系@TODO

        //先通过网关id查询mqtt_gateway_auth表中是否存在记录
        LambdaQueryWrapper<GatewayAuthEntity> wrapper = new QueryWrapper<GatewayAuthEntity>().lambda().
                eq(GatewayAuthEntity::getMqttGatewayId, id);
        //删除拓扑关系
        gatewayAuthMapper.delete(wrapper);

        //通过网关id查询mqtt_gateway_device表中是否存在记录
        LambdaQueryWrapper<GatewayDeviceEntity> wrapper2 = new QueryWrapper<GatewayDeviceEntity>().lambda().
                eq(GatewayDeviceEntity::getMqttGatewayId, id);

        gatewayDeviceMapper.delete(wrapper2);

        //删除网关表的记录
        gatewayMapper.deleteById(id);
        return Result.ok("删除网关成功");
    }

    //修改网关
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updateGateway(GatewayDTO gatewayDTO){

        //查询数据库中是否存在该设备
        GatewayEntity gatewayEntity = gatewayMapper.selectById(gatewayDTO.getId());

        if (Objects.isNull(gatewayEntity)){
            //不存在
            return Result.error("输入的网关id有误,更新失败");
        }

        //存在更新网关
        //通过dozer把gatewayDTO转为gatewayEntity
        gatewayEntity = mapper.map(gatewayDTO, GatewayEntity.class);

        //做更新
        gatewayMapper.updateById(gatewayEntity);
        return Result.ok("更新网关成功");
    }

    //通过网关sn号查询网关
    public Result queryGatewayBySNId(String snId){
        QueryWrapper<GatewayEntity> wrapper = new QueryWrapper<GatewayEntity>().eq("sn_id", snId);

        GatewayEntity gatewayEntity = gatewayMapper.selectOne(wrapper);

        return Result.ok(gatewayEntity);
    }

    //动态分页查询网关设备列表
    public IPage<GatewayVo> queryGatewayListByPage(GatewayDTO gatewayDTO){

        //创建分页实例
        IPage<GatewayVo> page = new Page<>(gatewayDTO.getPageNum(),gatewayDTO.getPageSize());

        //分页查询
        List<GatewayVo> gatewayVos = gatewayMapper.queryGatewayListByPage(page, gatewayDTO);

        page.setRecords(gatewayVos);

        return page;
    }

    //新增网关和mqtt认证参数的拓扑关系
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result createGatewayAuthTopo(GatewayDTO gatewayDTO){
        //判断网关是否存在
        GatewayEntity gatewayEntity = gatewayMapper.selectById(gatewayDTO.getId());
        if (Objects.isNull(gatewayEntity)){
            return Result.error("输入的网关id不存在");
        }

        //判断mqtt参数是否存在
        MqttAuthEntity mqttAuthEntity = mqttAuthMapper.selectById(gatewayDTO.getMqttAuthId());
        if (Objects.isNull(mqttAuthEntity)){
            return Result.error("输入的mqtt参数id不存在");
        }

        //先判断网关和mqtt参数在mqtt_gateway_auth中是否存在
        LambdaQueryWrapper<GatewayAuthEntity> wrapper = new QueryWrapper<GatewayAuthEntity>().lambda()
                .eq(GatewayAuthEntity::getMqttGatewayId, gatewayDTO.getId())
                .eq(GatewayAuthEntity::getMqttAuthId, gatewayDTO.getMqttAuthId());

        GatewayAuthEntity gatewayAuthEntity = gatewayAuthMapper.selectOne(wrapper);

        if (!Objects.isNull(gatewayAuthEntity)){
            //已存在拓扑关系
            return Result.error("该网关和mqtt认证参数存在拓扑关系,请勿重复创建");
        }

        //不存在,创建

        gatewayAuthEntity = mapper.map(gatewayDTO, GatewayAuthEntity.class);
        gatewayAuthEntity.setMqttGatewayId(gatewayDTO.getId());
        gatewayDTO.setId(null);//这一步是将前端传来的id去掉,以免使用mapper.map()匹配到gatewayAuthEntity中的id,导致不会自动生成32位的UUID
        gatewayAuthMapper.insert(gatewayAuthEntity);
        return Result.ok("创建网关和mqtt认证参数拓扑关系成功");
    }

    //删除网关和mqtt认证参数的拓扑关系
    public Result deleteGatewayAuthTopo(GatewayDTO gatewayDTO){
        //判断网关是否存在
        GatewayEntity gatewayEntity = gatewayMapper.selectById(gatewayDTO.getId());
        if (Objects.isNull(gatewayEntity)){
            return Result.error("输入的网关id不存在");
        }

        //判断mqtt参数是否存在
        MqttAuthEntity mqttAuthEntity = mqttAuthMapper.selectById(gatewayDTO.getMqttAuthId());
        if (Objects.isNull(mqttAuthEntity)){
            return Result.error("输入的mqtt参数id不存在");
        }

        //先判断网关和mqtt参数在mqtt_gateway_auth中是否存在
        LambdaQueryWrapper<GatewayAuthEntity> wrapper = new QueryWrapper<GatewayAuthEntity>().lambda()
                .eq(GatewayAuthEntity::getMqttGatewayId, gatewayDTO.getId())
                .eq(GatewayAuthEntity::getMqttAuthId, gatewayDTO.getMqttAuthId());

        GatewayAuthEntity gatewayAuthEntity = gatewayAuthMapper.selectOne(wrapper);

        if (Objects.isNull(gatewayAuthEntity)){
            //已存在拓扑关系
            return Result.error("该网关和mqtt认证参数不存在拓扑关系,删除失败");
        }

        //存在,删除
        gatewayAuthMapper.delete(wrapper);
        return Result.ok("删除网关和mqtt认证参数拓扑关系成功");
    }

    //新增网关和设备的拓扑关系
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result createGatewayDeviceTopo(GatewayDTO gatewayDTO){

        //判断网关是否存在
        GatewayEntity gatewayEntity = gatewayMapper.selectById(gatewayDTO.getId());
        if (Objects.isNull(gatewayEntity)){
            return Result.error("输入的网关id不存在");
        }

        //判断设备是否存在
        DeviceEntity deviceEntity = deviceMapper.selectById(gatewayDTO.getMqttDeviceId());
        if (Objects.isNull(deviceEntity)){
            return Result.error("输入的设备id不存在");
        }

        //先判断网关和mqtt参数在mqtt_gateway_device中是否存在
        LambdaQueryWrapper<GatewayDeviceEntity> wrapper = new QueryWrapper<GatewayDeviceEntity>().lambda()
                .eq(GatewayDeviceEntity::getMqttGatewayId, gatewayDTO.getId())
                .eq(GatewayDeviceEntity::getMqttDeviceId, gatewayDTO.getMqttDeviceId());

        GatewayDeviceEntity gatewayDeviceEntity = gatewayDeviceMapper.selectOne(wrapper);

        if (!Objects.isNull(gatewayDeviceEntity)){
            //已存在拓扑关系
            return Result.error("该网关和设备存在拓扑关系,请勿重复创建");
        }

        //不存在,创建

        gatewayDeviceEntity = mapper.map(gatewayDTO, GatewayDeviceEntity.class);
        gatewayDeviceEntity.setMqttGatewayId(gatewayDTO.getId());
        gatewayDTO.setId(null);//这一步是将前端传来的id去掉,以免使用mapper.map()匹配到gatewayAuthEntity中的id,导致不会自动生成32位的UUID

        gatewayDeviceMapper.insert(gatewayDeviceEntity);
        return Result.ok("创建网关和设备拓扑关系成功");
    }

    //删除网关和设备的拓扑关系
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deleteGatewayDeviceTopo(GatewayDTO gatewayDTO){

        //判断网关是否存在
        GatewayEntity gatewayEntity = gatewayMapper.selectById(gatewayDTO.getId());
        if (Objects.isNull(gatewayEntity)){
            return Result.error("输入的网关id不存在");
        }

        //判断设备是否存在
        DeviceEntity deviceEntity = deviceMapper.selectById(gatewayDTO.getMqttDeviceId());
        if (Objects.isNull(deviceEntity)){
            return Result.error("输入的设备id不存在");
        }

        //先判断网关和mqtt参数在mqtt_gateway_device中是否存在
        LambdaQueryWrapper<GatewayDeviceEntity> wrapper = new QueryWrapper<GatewayDeviceEntity>().lambda()
                .eq(GatewayDeviceEntity::getMqttGatewayId, gatewayDTO.getId())
                .eq(GatewayDeviceEntity::getMqttDeviceId, gatewayDTO.getMqttDeviceId());

        GatewayDeviceEntity gatewayDeviceEntity = gatewayDeviceMapper.selectOne(wrapper);

        if (Objects.isNull(gatewayDeviceEntity)){
            //已存在拓扑关系
            return Result.error("该网关和设备不存在拓扑关系,删除失败");
        }

        //存在,删除
        gatewayDeviceMapper.delete(wrapper);
        return Result.ok("删除网关和设备拓扑关系成功");
    }

    //通过网关id查询mqtt参数列表
    public IPage<MqttAuthVo> queryMqttAuthListByGatewayId(GatewayDTO gatewayDTO){

        //创建一个分页实例
        IPage<MqttAuthVo> page = new Page<>(gatewayDTO.getPageNum(),gatewayDTO.getPageSize());

        //分页查询
        List<MqttAuthVo> mqttAuthVos = gatewayMapper.queryMqttAuthListByGatewayId(page, gatewayDTO);

        page.setRecords(mqttAuthVos);

        return page;
    }

    //通过网关id查询设备列表
    public IPage<DeviceVo> queryDeviceListByGatewayId(GatewayDTO gatewayDTO){

        //创建一个分页实例
        IPage<DeviceVo> page = new Page<>(gatewayDTO.getPageNum(),gatewayDTO.getPageSize());

        //分页查询
        List<DeviceVo> mqttAuthVos = gatewayMapper.queryDeviceListByGatewayId(page, gatewayDTO);

        page.setRecords(mqttAuthVos);

        return page;
    }

}
